var echats_pressure = document.getElementById('echats_pressure');
var myChart = echarts.init(echats_pressure);

var _testerId = null;
var _tenantId = null;
var _productId = null;
var _boxId = null;
var _flag = null;
//用于移动的定位
var _longitude = null;
var _latitude = null;
var intervalTime = null;

function getDeviceParameter(testerId, tenantId, productId, boxId, flag) {
    _testerId = testerId;
    _tenantId = tenantId;
    _productId = productId;
    _boxId = boxId;
    _flag = flag;
    if (_flag == 0) {//正常测试
        singleTestResultQuery(testerId, tenantId, productId, boxId);
    } else {//压力测试
        PressureTestResultsQuery(testerId, tenantId, productId, boxId);
    }
    intervalTime = setInterval(function () {
        getDeviceStatusToMtms(tenantId, boxId);//实时工况
        if (_flag == 0) {//正常测试
            singleTestResultQuery(testerId, tenantId, productId, boxId);
        } else {//压力测试
            PressureTestResultsQuery(testerId, tenantId, productId, boxId);
        }
    }, 5000);
    //getDeviceByPosition(tenantId, boxId);//设备位置
}

function clearIntervalTime() {
    window.clearInterval(intervalTime);
}

hyneticMap();
// getDeviceParameter(1,160,1,'0100050000005',0);
// getDeviceParameter(1, 160, 5249, '0100050000008', 1);

//-in:{"testUserId":"1","testTenantId":"160","produceId":"5249","boxId":"0100050000008"}


function PressureTestResultsQuery(testerId, tenantId, productId, boxId) {
    $.ajax({
        type: "get",
        url: common.PressureTestResultsQueryUrl,//压力测试
        data: {
            "testUserId": testerId,
            "testTenantId": tenantId,
            "productId": productId,
            "boxId": boxId
        },
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var resultDate = result;
            console.log(resultDate);
            if (resultDate.code == 0) {
                var data = resultDate.data;
                var list = data.reason;
                var successAllCount = parseInt(data.upSuccess) + parseInt(data.downSuccess);
                var echatsArray = [];
                $('#getTesterByPressure').show();
                $('#boxId').text(data.boxId);//设备编码
                $('#upSuccess').text(data.upSuccess);//上报成功次数
                $('#upFail').text(data.upFail);//上报失败次数
                $('#downSuccess').text(data.downSuccess);//下发成功次数
                $('#downFail').text(data.downFail);//下发失败次数
                $('#alreadyTime').text(data.alreadyTime);//已进行时间
                for (var key in list) {//转换成map用的json格式
                    var _o = {};
                    var name = common.deviceErrorMsg[key] + ":" + list[key];
                    if (name.length >= 5) {
                        var _f = name.substr(0, 5);
                        var _l = name.substr(5, name.length);
                        name = _f + '\n' + _l;
                    }
                    // _o.name = common.deviceErrorMsg[key] + ":" + list[key];
                    _o.name = name;
                    _o.value = parseInt(list[key]);
                    if (_o.value > 0) {
                        echatsArray.push(_o);
                    }
                }

                echatsArray.push({"name": "成功:" + successAllCount, "value": successAllCount});
                getTesterByPressureForEchats(echatsArray);
            } else {
                common.showMsgTips(resultDate.msg);
            }
        }, error: function () {
            console.log('报错')
        }
    });
}


function getTesterByPressureForEchats(bean) {
    option = {
        title: {
            text: '某站点用户访问来源',
            subtext: '纯属虚构',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        series: [
            {
                name: '访问来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: bean,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
// 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
    window.onresize = myChart.resize;
    resizeWorldMapContainer();

}

//用于使chart自适应高度和宽度,通过窗体高宽计算容器高宽
var resizeWorldMapContainer = function () {
    echats_pressure.style.width = window.innerWidth + 'px';
    echats_pressure.style.height = window.innerHeight + 'px';
    myChart.resize();
    //rem单位
    //worldMapContainer.style.width = window.innerWidth/750*690+'px';
    //worldMapContainer.style.height = window.innerHeight/750*600+'px';
};
//设置容器高宽
resizeWorldMapContainer();

function singleTestResultQuery(testerId, tenantId, productId, boxId) {
    $.ajax({
        type: "get",
        url: common.singleTestResultQueryUrl,
        data: {
            "boxId": boxId
        },
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var resultDate = result;
            if (resultDate.code == 0) {
                var data = resultDate.data;
                var newDeviceType = data.newDeviceType;
                var newDeviceType = data.oldDeviceType;
                $('#getTesterByDevice').show();
                $('#boxId').text(data.boxId);//设备编码
                // $('#upSuccess').text(data.upSuccess);//上报成功次数
                // $('#upFail').text(data.upFail);//上报失败次数
                // $('#alreadyTime').text(data.alreadyTime);//已进行时间
                //转换成map用的json格式
                assembleDevuceStatus('status1', data.status1, data.encode);
                assembleDevuceStatus('status2', data.status2, data.encode);
                assembleDevuceStatus('status3', data.status3, data.encode);
                assembleDevuceStatus('status4', data.status4, data.encode);
                assembleDevuceStatus('status5', data.status5, data.encode);
                // assembleDevuceStatus('status6', data.status6);
                /*assembleDevuceStatus('status7', data.status7);
                assembleDevuceStatus('status8', data.status8);*/
            } else {
                common.showMsgTips(resultDate.msg);
            }
        }
    });
}

function assembleDevuceStatus(key, status, encode) {
    if (status) {
        $('#' + key).find('.node-content').text(common.deviceErrorMsg[status]);
        $('#' + key).find('.node-icon').find('i').css('color', "#508EFA");
        $('#' + key).find('.node-line').css('background', "#508EFA");
    } else {
        if (encode != null && encode.length > 0) {
            $('#' + key).find('.node-content').text(common.deviceErrorMsg[encode]);
            $('#' + key).find('.node-icon').find('i').css('color', "red");
            $('#' + key).find('.node-line').css('background', "red");
        } else {
            $('#' + key).find('.node-icon').find('i').css('color', "#ddd");
            $('#' + key).find('.node-line').css('background', "#ddd");
        }
    }

    if (status == 602) {
        $('#upSuccess').text(1);
        $('#upFail').text(0);//上报失败次数
    } else if (status == 661) {
        $('#upSuccess').text(0);
        $('#upFail').text(1);//上报失败次数
    } else if (status == 603) {
        $('#status6').find('.node-content').text("测试成功");
        $('#status6').find('.node-icon').find('i').css('color', "#508EFA");
        $('#status6').find('.node-line').css('background', "#508EFA");
        $('#downSuccess').text(1);
        $('#downFail').text(0);//上报失败次数
    } else if (status == 664) {
        $('#downSuccess').text(0);
        $('#downFail').text(1);//上报失败次数
    }
}